﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;

namespace DataRecon
{
    public class GetTime
    {
        public static DateTime FindTime(string[] notification, DateTime inputDate)
        {
            TimeSpan time = TimeSpan.Zero;
            int lenght = notification.Length;
            TimeSpan correction = TimeSpan.Zero;
            CultureInfo provider = CultureInfo.CreateSpecificCulture("ru-RU");
            try
            {
                for (int i = 0; i < lenght-1; i++)
                {
                    if (notification[i] == Literals.CHEREZ || notification[i] == Literals.CHEREZ2)
                    {
                        if (notification[i+2].Contains(Literals.HOUR))
                        {
                            TimeSpan.TryParseExact(notification[i + 1], "%h", provider, out time);
                            inputDate = inputDate + time;
                        }
                        else if (notification[i + 2].Contains(Literals.MINUTE))
                        {
                            TimeSpan.TryParseExact(notification[i + 1], "%m", provider, out time);
                            inputDate = inputDate + time;
                        }
                    }

                    if (notification[i] == Literals.IN || notification[i] == Literals.IN2)
                    {
                        if (notification[i + 1].Contains(Literals.DOTS))
                        {
                            TimeSpan.TryParseExact(notification[i + 1], "g", provider, out time);
                            inputDate = inputDate.Date + time;
                        }
                        else if (notification[i + 1].Length <= 2)
                        {
                            TimeSpan.TryParseExact(notification[i + 1], "%h", provider, out time);
                            inputDate = inputDate.Date + time;
                        }

                    }
                    if (notification[i].Contains(Literals.HOUR) && notification[i + 2].Contains(Literals.MINUTE))
                    {
                        TimeSpan.TryParseExact(notification[i + 1], "%m", provider, out correction);
                        time = time + correction;
                        inputDate = inputDate.Date + time;
                    }
                }
            }
            catch
            {
                Console.WriteLine("Error in time find function.");
            }
            //Console.WriteLine("{0} ", time);
            return inputDate;
        }
    }
}
